package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.model.BaseRegistration;
import com.redoor.app.stake.basicdata.model.BaseRegistrationApply;
import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.service.BaseRegistrationApplyService;
import com.redoor.app.stake.basicdata.service.BaseRegistrationRedisService;
import com.redoor.app.stake.basicdata.service.BaseRegistrationService;
import com.redoor.app.stake.basicdata.service.BaseSystemOperationLogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.service.BaseWorkorderOperationLogService;
import com.redoor.app.stake.workflow.service.BaseWorkorderService;
import com.redoor.app.stake.workflow.service.MaintenanceApplyWorkOrderService;
import com.redoornetwork.framework.core.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaintenanceApplyWorkOrderServiceImpl implements MaintenanceApplyWorkOrderService {

    private static final Logger logger = LoggerFactory.getLogger(MaintenanceApplyWorkOrderServiceImpl.class);

    @Autowired
    private BaseWorkorderService baseWorkorderService;

    @Autowired
    private BaseRegistrationService baseRegistrationService;

    @Autowired
    private BaseRegistrationApplyService baseRegistrationApplyService;

    @Autowired
    private BaseWorkorderOperationLogService operationLogService;

    @Autowired
    private BaseSystemOperationLogService baseSystemOperationLogService;

    @Autowired
    private BaseUserService userService;

    @Override
    public int create(BaseStake stake,String mqUuid) {
        if (null == stake
                || StringUtils.isBlank(stake.getUuid())
//                || StringUtils.isBlank()
                ){
            /*抛出参数为空*/
            logger.info("MaintenanceApplyWorkOrderServiceImpl_create_paramError");
            throw new ServiceException(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        BaseWorkorder workorder = new BaseWorkorder();
        /*生成uuid由公共的insert方法转移到这个地方,因为保存工单操作日志的时候需要这个uuid*/
        workorder.setUuid(UUIDTool.getUUID());
        /*保存工单类型*/
        workorder.setTargettype(WorkFlowStatus.WORKORDER_TARGET_TYPE_MAINTENANCEAPPLY.getCode());
        /*保存目标uuid*/
        workorder.setTargetuuid(stake.getUuid());
        /*保存工单类型名称*/
        workorder.setContent(WorkFlowStatus.WORKORDER_TARGET_TYPE_MAINTENANCEAPPLY.getMessage());

        /*****begin 功能描述:实现了工单内保存组织机构编码  author:zhangMumu add 2018/4/13 0013 17:32*****/
        /*通过桩内保存的注册码uuid,在redis里面拿到注册码实体*/
        BaseRegistration registration = baseRegistrationService.getRegistrationByUuid(stake.getRegistrationuuid());
        BaseRegistrationApply baseRegistrationApply = baseRegistrationApplyService.getBaseRegistrationApply(registration.getRegistrationapplyuuid());
        /*保存目标组织机构代码*/
        workorder.setCreateorgcode(baseRegistrationApply.getProrgcode());
        /*****end 功能描述:实现了工单内保存组织机构编码  author:zhangMumu add 2018/4/13 0013 17:32*****/
        
        /*创建人uuid存入接收到前置调试成功后的mq对应记录的uuid*/
        workorder.setCreatoruuid(mqUuid);
        /*添加操作日志*/
        if(!operationLogService.maApplyCreateLog(workorder)){
            logger.error("MaintenanceApplyWorkOrderServiceImpl_create_addMaApplyCreateLogError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.ADD,workorder.getUuid(),"",true);

        return baseWorkorderService.insert(workorder);
    }

    @Override
    public int submit(BaseWorkorder baseWorkorder) {
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_ACCEPT.getCode()));
        /*添加操作日志*/
        if (!operationLogService.maApplySubmitLog(baseWorkorder)){
            logger.error("MaintenanceApplyWorkOrderServiceImpl_submit_addMaApplySubmitLogError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);
        return baseWorkorderService.update(baseWorkorder);
    }

    @Override
    public int close(String targetUuid) {
        List<BaseWorkorder> baseWorkorders = baseWorkorderService.findMainApplyByTargetUuid(targetUuid);
        /*搜索结果倒序排列,取出第一条修改掉*/
        BaseWorkorder baseWorkorder = baseWorkorders.get(0);
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_FINISH.getCode()));
        /*添加操作日志*/
        if (!operationLogService.maApplyCloseLog(baseWorkorder)){
            logger.error("MaintenanceApplyWorkOrderServiceImpl_close_addMaCloseLogError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);
        return baseWorkorderService.update(baseWorkorder);
    }

    @Override
    public int active(String targetUuid) {
        List<BaseWorkorder> baseWorkorders = baseWorkorderService.findMainApplyByTargetUuid(targetUuid);
        BaseWorkorder baseWorkorder = baseWorkorders.get(0);
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_APPLYING.getCode()));
        /*添加操作日志*/
        if (!operationLogService.maApplyActiveLog(baseWorkorder)){
            logger.error("MaintenanceApplyWorkOrderServiceImpl_active_addMaApplyActiveLogError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);
        return baseWorkorderService.update(baseWorkorder);
    }
}
